export const ajax = (url, params, methos, loading) => {
	if(loading) {
		uni.showLoading({
			title: "加载中...",
			mask: true
		})
	}

	return new Promise((resolve, reject) => {
		let header = {
			'Content-Type': 'application/json; charset=UTF-8'
		};

		let Authorization = uni.getStorageSync('token'); 
		if (Boolean(Authorization)) {
			header['Authorization'] = Authorization;
		}
		
		uni.request({
			url: getApp().globalData.baseUrl + url,
			// url: "/api" + url,
			data: params,
			method: methos,
			header: header,
			success: res => {
				uni.hideLoading();
				/*
					200 正常
					201 未绑定手机号
					400 报错 
					401 未登录
				*/
				switch (res.data.code) {
					case 200:
						resolve(res.data);
						console.log("接口【 " + url + " 】请求成功！返回参数为 ↓↓↓");
						console.log(res.data);
						break;
					case 201:
						reject(res.data);
						console.log("接口【 " + url + " 】请求成功！状态码为：201(未绑定手机号)");
						break;
					case 400:
						reject(res.data);
						console.error("接口【 " + url + " 】请求错误！状态码为：400(报错)");
						break;
					case 401:
						reject(res.data);
						console.error("接口【 " + url + " 】请求错误！状态码为：401(未登录)");
						uni.clearStorageSync();
						// setTimeout(() => {
						// 	uni.reLaunch({
						// 		url: "/pages/login/login"
						// 	})
						// }, 1500)
						break;
					default:
						reject(res.data);
						console.error("接口【 " + url + " 】请求错误！状态码为：" + res.data.code);
						break;
				}
			},
			fail: error => {
				if(loading) {
					uni.hideLoading();
				}
				uni.showToast({
					title: "请求失败,请重试!",
					icon: "none"
				})
				reject(error);
			}
		})
	})
}
